package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SignonActionServlet extends HttpServlet {

    private static final String VIEW_MAIN = "/WEB-INF/jsp/catalog/Main.jsp";
    private static final String VIEW_SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";
    private static final String VIEW_ERROR = "/WEB-INF/jsp/common/Error.jsp";
    private AccountService accountService;
    private CatalogService catalogService;
    private Account account = null;
    private List<Product> productList;
    private boolean authenticated;
    private String username;
    private String password;
    private Integer currentOrderId;
    private LogService logService;
    private String imageText;
    private String text;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        /*imageText = request.getParameter("image");
        text = (String) session.getAttribute("text");
        if (!text.equalsIgnoreCase(imageText)){
            request.setAttribute("imageMess", "Invalid Code!");
            logService = new LogService();
            logService.insertLog("Invalid VerifyCode","Invalid VerifyCode!", "null");
            request.getRequestDispatcher(VIEW_SIGNON).forward(request,response);
        } else*/ {
            accountService = new AccountService();
            catalogService = new CatalogService();
            //account = new Account();
            productList = new ArrayList<Product>();
            authenticated = false;

            username = request.getParameter("username");
            password = request.getParameter("password");

            account = accountService.getAccount(username, password);

            if (account == null) {
                session.setAttribute("message", "Invalid username or password.  Signon failed.");
                logService = new LogService();
                logService.insertLog("Sign in Failed","Sign in Failed!", "null");
                request.getRequestDispatcher(VIEW_ERROR).forward(request, response);
            } else {
                account.setPassword(null);
                productList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
                authenticated = true;

                session.setAttribute("account", account);
                //System.out.println(account.getFavouriteCategoryId());
                session.setAttribute("myList", productList);
                session.setAttribute("authenticated", authenticated);
                session.setAttribute("username",username);
                logService = new LogService();
                logService.insertLog("Sign in","Username:"+account.getUsername(), account.getUsername());
//                currentOrderId = (Integer) session.getAttribute("currentOrderId");
//                if (currentOrderId == null) {
//                    currentOrderId = new Integer(1000);
//                    session.setAttribute("currentOrderId", currentOrderId);
//                }
                request.getRequestDispatcher(VIEW_MAIN).forward(request, response);
            }
        }
    }}

